<?php

require_once("./config.php");
mysql_connect("localhost", "vdl", "25chris11") or die("Keine Verbindung moeglich");
mysql_select_db("vdl_roleplay") or die("Die Datenbank existiert nicht.");

class SuccessMessage {

    public $status;
    public $message;
    public $debugging;

}

function getDetails($int) {
    $sql = "SELECT * FROM  `character` where `_id` = " . $int;
    $query = MySql::query($sql);
    while ($row = mysql_fetch_object($query)) {
        $character = new Character();
        return $character->deserialize($row);
    }
}

function addCharacter($character) {
    $sql = "INSERT INTO `vdl_roleplay`.`character` 
        (`_id`, `_cid`, `_oid`, `_rid`, `surname`, `lastname`, `age`, `gender`, `race`, `portrait`, `status`, `profession`, `type`, `task`, `class`, `shortdescription`) 
        VALUES 
        (NULL, $character->_cid, $character->_oid, $character->_rid, " .
            $character->surname
            . ", " .
            $character->lastname
            . ", " .
            $character->age
            . ", " .
            $character->gender
            . ", " .
            $character->race
            . ", " .
            $character->portrait
            . ", " .
            $character->status
            . ", " .
            $character->profession
            . ", " .
            $character->type
            . ", " .
            $character->task
            . ", " .
            $character->class
            . ", " .
            $character->shortdescription
            . ");";
    MySql::insert($sql);
    return mysql_affected_rows();
}

function deleteCharacter($int) {
    $sql = "DELETE FROM  `character` where `_id` = " . $int;
    MySql::query($sql);
    return mysql_affected_rows();
}

function prepareData($array) {
    foreach ($array as $key => $value) {
        if ($value == null)
            $array[$key] = "NULL";
        if ($value == 0) {
            if ($key == "_cid" || $key == "_oid" || $key == "_rid")
                $array[$key] = "NULL";
        }
    }

    $sql = (Object) $array;
    return $sql;
}

function returnSuccessMessage($response) {
    $success = new SuccessMessage();
    switch ($response) {
        case 1:
            $success->status = 200;
            $success->message = $response;
            break;
        case 0:
            $success->status = 200;
            $success->message = $response;
            break;
        case -1:
            $success->status = 500;
            $success->message = mysql_error();
            break;
        default:
            $success->status = 500;
            $success->message = "Service not found: " . $response;
    }
    return $success;
}

$service = null;

if (isset($_GET['qqfile'])) {
    $service = "upload";
}

if (isset($_GET['method'])) {
    $service = $_GET['method'];
}
switch ($service) {
    /*
     * upload Character image
     */
    case 'upload':
        $allowedExtensions = array();
        $sizeLimit = 10 * 1024 * 1024;

        $uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
        $result = $uploader->handleUpload($path.'res/img_char/');
        echo htmlspecialchars(json_encode($result), ENT_NOQUOTES);
        break;
    /*
     * receive Character data
     */
    case 'getCharacter':
        echo json_encode(controllerCharacter::get($_GET['id'],$_GET['type']));
        break;
    /*
     * add Rank
     */
    case 'addRank':
        $array = array(
            'name' => html_entity_decode($_GET['name'], ENT_QUOTES, 'UTF-8'),
            '_oid' => $_GET['_oid'],
            '_rid' => $_GET['_rid']
        );
        $response = controllerRank::add(prepareData($array),$_GET['type']);
        echo json_encode(returnSuccessMessage($response));
        break;
    /*
     * add Organisation
     */
    case 'addOrganisation':
        $array = array(
            'name' => html_entity_decode($_GET['name'], ENT_QUOTES, 'UTF-8'),
            '_oid' => $_GET['_oid'],
            '_cid' => $_GET['_cid']
        );
        $response = controllerOrganisation::add(prepareData($array));
        echo json_encode(returnSuccessMessage($response));
        break;
    /*
     * add Character
     */
    case 'addCharacter':
        $array = array(
            '_cid' => $_GET['_cid'],
            '_oid' => $_GET['_oid'],
            '_rid' => $_GET['_rid'],
            'surname' => html_entity_decode($_GET['surname'], ENT_QUOTES, 'UTF-8'),
            'lastname' => html_entity_decode($_GET['lastname'], ENT_QUOTES, 'UTF-8'),
            'age' => $_GET['age'],
            'gender' => $_GET['gender'],
            'race' => html_entity_decode($_GET['race'], ENT_QUOTES, 'UTF-8'),
            'portrait' => html_entity_decode($_GET['portrait'], ENT_QUOTES, 'UTF-8'),
            'status' => html_entity_decode($_GET['status'], ENT_QUOTES, 'UTF-8'),
            'profession' => html_entity_decode($_GET['profession']),
            'type' => $_GET['type'],
            'task' => html_entity_decode($_GET['task'], ENT_QUOTES, 'UTF-8'),
            'class' => html_entity_decode($_GET['klasse']),
            'shortdescription' => html_entity_decode($_GET['description'], ENT_QUOTES, 'UTF-8')
        );
        $response = controllerCharacter::add(prepareData($array));
        echo json_encode(returnSuccessMessage($response));
        break;
    /*
     * update Character
     */
    case 'updateCharacter':
        $array = array(
            '_id' => $_GET['_id'],
            '_cid' => $_GET['_cid'],
            '_oid' => $_GET['_oid'],
            '_rid' => $_GET['_rid'],
            'surname' => html_entity_decode($_GET['surname'], ENT_QUOTES, 'UTF-8'),
            'lastname' => html_entity_decode($_GET['lastname'], ENT_QUOTES, 'UTF-8'),
            'age' => $_GET['age'],
            'gender' => $_GET['gender'],
            'race' => html_entity_decode($_GET['race'], ENT_QUOTES, 'UTF-8'),
            'portrait' => html_entity_decode($_GET['portrait'], ENT_QUOTES, 'UTF-8'),
            'status' => html_entity_decode($_GET['status'], ENT_QUOTES, 'UTF-8'),
            'profession' => html_entity_decode($_GET['profession']),
            'type' => $_GET['type'],
            'task' => html_entity_decode($_GET['task'], ENT_QUOTES, 'UTF-8'),
            'class' => html_entity_decode($_GET['klasse']),
            'shortdescription' => html_entity_decode($_GET['description'], ENT_QUOTES, 'UTF-8')
        );
        $response = controllerCharacter::update(prepareData($array));
        echo json_encode(returnSuccessMessage($response));
        break;
    /*
     * delete Character
     */
    case "deleteCharacter":
        $response = controllerCharacter::delete($_GET['id']);
        echo json_encode(returnSuccessMessage($response));
        break;
    case "deleteOrganisation":
        $response = controllerOrganisation::delete($_GET['id']);
        echo json_encode(returnSuccessMessage($response));
        break;
    default:
        echo json_encode(returnSuccessMessage(null));
        break;
}
?>
